﻿namespace MtmDeepCopy
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Xml;
    using Sogeti.QueryWrapper;
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MTMViewModel vm;

        public MainWindow()
        {
            InitializeComponent();

            vm = new MTMViewModel();
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (ConfigurationManager.AppSettings.Keys.Count > 0)
            {
                Uri sourceUri = new Uri(ConfigurationManager.AppSettings["SourceUri"]);
                string sourceTP = ConfigurationManager.AppSettings["SourceTeamProject"];

                Uri targetUri = new Uri(ConfigurationManager.AppSettings["SourceUri"]);
                string targetTP = ConfigurationManager.AppSettings["TargetTeamProject"];
                vm.Load(sourceUri, sourceTP, targetUri, targetTP);
            }
            else
            {
                vm.cmdConnectSourceTP();
                if (vm.SourceTestPlan.TeamProjectCollection != null)
                {
                    vm.TargetTestPlan.TeamProjectCollection = vm.SourceTestPlan.TeamProjectCollection;
                    vm.TargetTestPlan.TeamProject = vm.SourceTestPlan.TeamProject;
                }
            }

            DataContext = vm;
        }

        public MainWindow(MTMViewModel vm)
        {
            InitializeComponent();
            DataContext = vm;
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void cmdOpenSourceTeamProject_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdConnectSourceTP();
        }
        
        private void cmdOpenTargetTeamProject_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdConnectTargetTP();
        }

        private void cmdCopy_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdDoActions(this);
        }

        private void cmdAddDeepCopyNode_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdAddDeepCopyNode((XmlElement)treeViewSource.SelectedItem, (XmlElement)treeViewTarget.SelectedItem);
            vm.cmdDoActions(this);
        }

        private void cmdAddSallowCopyNode_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdAddShallowCopyNode((XmlElement)treeViewSource.SelectedItem, (XmlElement)treeViewTarget.SelectedItem);
            vm.cmdDoActions(this);
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void treeViewTarget_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            vm.TargetTestPlan.SelectedNode = treeViewTarget.SelectedItem as XmlElement;
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
        }

        private void cmdAddTCLinkedQuery_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdAddTCLinkedQueryAction((WIQLQueryItems)treeViewQueries.SelectedItem, (XmlElement)treeViewTarget.SelectedItem);
            vm.cmdDoActions(this);
        }

        private void cmdAddField_Click(object sender, RoutedEventArgs e)
        {
            vm.AddFieldRow();
        }

        private void cmdNewStaticSuite_Click(object sender, RoutedEventArgs e)
        {
            vm.cmdNewStaticSuite();
            vm.cmdDoActions(this);
        }

        private void treeViewTarget_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F2)
            {
                ((XmlElement)treeViewTarget.SelectedItem).SetAttribute("Edit", "true");
            }
        }

        private void treview_Node_Text_Changed(object sender, RoutedEventArgs e)
        {
            vm.UpdateSuiteName(((XmlElement)treeViewTarget.SelectedItem));
            vm.cmdDoActions(this);
        }

        private void wndMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MTMViewModel vm = (MTMViewModel)DataContext;
            
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (vm.SourceTeamProject != string.Empty)
            {
                conf.AppSettings.Settings.Add("SourceUri", vm.SourceTeamPojectCollectionUri);
                conf.AppSettings.Settings.Add("SourceTeamProject", vm.SourceTeamProject);
            }

            if (vm.TargetTeamProject != string.Empty)
            {
                conf.AppSettings.Settings.Add("TargetUri", vm.TargetTeamPojectCollectionUri);
                conf.AppSettings.Settings.Add("TargetTeamProject", vm.TargetTeamProject);
            }

            conf.Save(ConfigurationSaveMode.Modified);
        }
    }
}
